// pages/findcar/findcar.js
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk = new QQMapWX({
    key: 'NOVBZ-ECFCS-GC6OG-6ZMSN-2RPGJ-52FYI'
});
//  存在问题总结：
//  1. 微信show-location 获取到的当前位置点存在误差
//  2. 

Page({

    /**
     * 页面的初始数据
     */
    data: {
        carInfo: [],
        mapCtx: "",
        longitude: "",
        latitude: '',
        polyline: '',
        markers: '',
        currentLat: '',
        currentLong: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        const that = this
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.on('carInfo', function(res) {
            that.setData({
                carInfo: res.data
            })
        })
        this.mapCtx = wx.createMapContext('map')
        wx.getLocation({ //微信获取当前位置
            type: 'gcj02',
            isHighAccuracy: 'true',
            success(res) {
                console.log(res)
                var marker = [{
                        latitude: that.data.carInfo.latitude,
                        longitude: that.data.carInfo.longitude,
                        iconPath: '../../images/map/location.png',
                        width: 20,
                        height: 20
                    },

                ]
                that.setData({
                    latitude: res.latitude,
                    longitude: res.longitude,
                    markers: marker
                })
                that.getWay()
            }
        })
    },
    getWay() {
        const that = this
        let end = that.data.markers[0].latitude + ',' + that.data.markers[0].longitude
            // console.log(end)
        qqmapsdk.direction({
            mode: 'walking',
            from: '',
            to: end,
            success: function(res) {
                console.log(res)
                var ret = res;
                var coors = ret.result.routes[0].polyline, // 接受折点坐标
                    pl = [];
                //坐标解压（返回的点串坐标，通过前向差分进行压缩）
                var kr = 1000000;
                // console.log(coors)
                for (var i = 2; i < coors.length; i++) {
                    coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
                }
                if (coors.length <= 6) { // 当所给路径小于三次折点时  直线指向
                    pl.push({
                        latitude: that.data.latitude,
                        longitude: that.data.longitude
                    })
                    pl.push({
                        latitude: that.data.markers[0].latitude,
                        longitude: that.data.markers[0].longitude
                    })
                    that.setData({
                        latitude: pl[0].latitude,
                        longitude: pl[0].longitude,
                        polyline: [{
                            points: pl,
                            color: '#FF0000DD',
                            width: 4
                        }]
                    })
                } else {
                    for (var i = 0; i < coors.length; i += 2) {
                        pl.push({ latitude: coors[i], longitude: coors[i + 1] })
                    }
                    console.log(pl)
                    that.setData({
                        latitude: pl[0].latitude,
                        longitude: pl[0].longitude,
                        polyline: [{
                            points: pl,
                            color: '#FF0000DD',
                            width: 4
                        }]
                    })
                }
            },
            fail: function(error) {
                console.error(error);
            },
        })
    },
    found() { //找到自己的车 修改车的状态位nostop
        const that = this
        console.log('前端foundFunction')
        console.log(that.data.carInfo.carid)
        wx.cloud.callFunction({ // 调取云函数 获取用户车辆信息
            data: { carid: that.data.carInfo.carid }, //传输车的id
            name: 'foundcar',
            success: res => {
                console.log('调取foundcar云函数成功', res.result)
                if (res.result == 'success') {
                    wx.switchTab({
                        url: '../car/car' //页面跳转
                    })
                }
            },
            fail: res => {
                console.log(res)
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})